package com.example.everything_is_possible;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import com.example.everything_is_possible.adapter.LengthItem1Adapter;

public class WeightActivity extends AppCompatActivity {
    public static final String TAG = " weight";
    private TextView unit1,unit2;
    private ImageView show1,show2;
    private EditText edt1,edt2;
    private TextView txt1,txt2;

    private RecyclerView numRec;
    private NumAdapter numAdapter;
    private RecyclerView lengthItemRec1;
    private LengthItem1Adapter item1Adapter1;
    private Dialog dialog1;

    private RecyclerView lengthItemRec2;
    private LengthItem1Adapter item1Adapter2;
    private Dialog dialog2;
    private String value1;
    private String value2;
    private String num1;
    private String num2;
    private String str2;
    private BigDecimal a;
    private String s1;

    private Button mbtn_back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weight);

        mbtn_back = findViewById(R.id.btn_back2);

        mbtn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initView();

    }

    private void initView() {
        txt1 = findViewById(R.id.txt1);
        txt2 = findViewById(R.id.txt2);
        unit1 = findViewById(R.id.unit1);
        unit2 = findViewById(R.id.unit2);

        show1 = findViewById(R.id.show1);
        show2 = findViewById(R.id.show2);

        edt1 = findViewById(R.id.edt1);
        edt2 = findViewById(R.id.edt2);

        numRec = findViewById(R.id.numRec);
        LinearLayoutManager manager = new GridLayoutManager(getBaseContext(),3);
        numRec.setLayoutManager(manager);

        num1 = edt1.getText().toString();
        num2 = edt2.getText().toString();

        edt1.requestFocus();
        edt1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int inputType = edt1.getInputType();
                edt1.setInputType(InputType.TYPE_NULL);// 让系统键盘不弹出
                edt1.setInputType(inputType);



                return false;
            }
        });
        closeInputMethod();

        edt2.requestFocus();
        edt2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int inputType = edt2.getInputType();
                edt2.setInputType(InputType.TYPE_NULL);// 让系统键盘不弹出
                edt2.setInputType(inputType);
                return false;
            }
        });
        edt2.clearFocus();
        edt2.setFocusable(false);

        //键盘
        initValue();

    }
    private void closeInputMethod() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示
            imm.hideSoftInputFromWindow(edt1.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    private void initValue() {
        String[] str = {"7","8", "9", "4","5","6", "1", "2", "3",".", "0","删除"};

        final List<Strings> stringsList = new ArrayList<>();
        for(int i = 0; i < str.length; i++) {
            Strings strings1 = new Strings();
            strings1.setNum(str[i]);
            stringsList.add(strings1);
        }
        numAdapter = new NumAdapter(stringsList,getBaseContext());
        numRec.setAdapter(numAdapter);

        edt1.setText("");
        numAdapter.MyItemOnclick(new MyItemOnclick() {
            @Override
            public void onItemClick(View view, int position, String str) {

                num1 = edt1.getText().toString();
                num2 = edt2.getText().toString();
                if (num1.equals("0")){
                    edt1.setText("");
                    edt2.setText("");
                }
                if (position!=stringsList.size()-1){
                    insertText(edt1,str);
                    num1 = edt1.getText().toString();
                    num2 = edt2.getText().toString();

                    initStart();
                }else {
                    num1 = edt1.getText().toString();
                    num2 = edt2.getText().toString();
                    int index=edt1.getSelectionStart();   //获取Edittext光标所在位置
                    String strs=edt1.getText().toString();

                    if(strs != null && strs.length() != 0) {
                        //先判断是不是对象，如果是，再判断是不是空字符串
                        if (strs.length()>1){
                            edt1.getText().delete(index-1,index);
                            num1 = edt1.getText().toString();
                            a = new BigDecimal(num1);
                            initStart();
                        }else {
                            edt1.setText("0");
                            num1 = edt1.getText().toString();
                            a = new BigDecimal(num1);
                            initStart();
                        }
                    }

                }
            }
        });
    }


    @Override
    protected void onStart() {
        super.onStart();

        show1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(WeightActivity.this);
                LayoutInflater inflater = LayoutInflater.from(getBaseContext());
                View  view1 = inflater.inflate(R.layout.length,null);
                builder.setView(view1);

                lengthItemRec1 = view1.findViewById(R.id.lengthItemRec);
                LinearLayoutManager manager = new GridLayoutManager(getBaseContext(),1);
                lengthItemRec1.setLayoutManager(manager);
                //添加list
                initItemValue1();

                dialog1 = builder.create();
                dialog1.show();
                //dialog大小
                WindowManager.LayoutParams params =
                        dialog1.getWindow().getAttributes();
                params.width = 700;
                params.gravity = Gravity.CENTER;

                dialog1.getWindow().setAttributes(params);
            }
        });
        show2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(WeightActivity.this);
                LayoutInflater inflater = LayoutInflater.from(getBaseContext());
                View  view2 = inflater.inflate(R.layout.length,null);
                builder.setView(view2);

                lengthItemRec2 = view2.findViewById(R.id.lengthItemRec);
                LinearLayoutManager manager = new GridLayoutManager(getBaseContext(),1);
                lengthItemRec2.setLayoutManager(manager);
                //添加list
                initItemValue2();

                dialog2 = builder.create();
                dialog2.show();
                //dialog大小
                WindowManager.LayoutParams params =
                        dialog2.getWindow().getAttributes();
                params.width = 700;
                params.gravity = Gravity.CENTER;

                dialog2.getWindow().setAttributes(params);

            }
        });

        initStart();
    }
    private void initStart(){
        s1 = txt1.getText().toString();
        str2 = txt2.getText().toString();
        a = new BigDecimal(num1);

        if (s1.equals("吨")){
            unit1.setText("");
            unit1.setText("t");
            initValt();
        }
        if (s1.equals("千克")){
            unit1.setText("");
            unit1.setText("kg");
            initValkg();
        }
        if (s1.equals("克")){
            unit1.setText("");
            unit1.setText("g");
            initValg();
        }
        if (s1.equals("微克")){
            unit1.setText("");
            unit1.setText("μg");
            initValμg();
        }
        if (s1.equals("毫克")){
            unit1.setText("");
            unit1.setText("mg");
            initValmg();
        }
        if (s1.equals("盎司")){
            unit1.setText("");
            unit1.setText("oz");
            initValoz();
        }
        if (s1.equals("克拉")){
            unit1.setText("");
            unit1.setText("ct");
            initValct();
        }

    }
    //  "克拉 ct"
    private void initValct(){
        if (str2.equals("吨")){
            BigDecimal b = new BigDecimal(0.0000002);
            BigDecimal divide = a.multiply(b);
            BigDecimal d =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(d+"");
        }
        if (str2.equals("千克")){
            BigDecimal b = new BigDecimal(0.0002);
            BigDecimal divide = a.divide(b);
            BigDecimal d =divide.setScale(6, RoundingMode.HALF_UP);
            edt2.setText(d+"");

        }
        if (str2.equals("克")){
            BigDecimal b = new BigDecimal(0.2);
            BigDecimal divide = a.divide(b);
            BigDecimal d =divide.setScale(3, RoundingMode.HALF_UP);
            edt2.setText(d+"");

        }
        if (str2.equals("微克")){
            BigDecimal b = new BigDecimal(200000);
            BigDecimal divide = a.multiply(b);
            BigDecimal d =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(d+"");

        }
        if (str2.equals("毫克")){
            BigDecimal b = new BigDecimal(200);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("盎司")){
            BigDecimal b = new BigDecimal(0.0070548);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(6, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("克拉")){
            edt2.setText(a +"");

        }
    }

    //  "盎司 oz"
    private void initValoz(){
        if (str2.equals("吨")){
            BigDecimal b = new BigDecimal(0.0000283);
            BigDecimal divide = a.multiply(b);
            BigDecimal d =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(d+"");
        }
        if (str2.equals("千克")){
            BigDecimal b = new BigDecimal(0.0283495);
            BigDecimal divide = a.divide(b);
            BigDecimal d =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(d+"");

        }
        if (str2.equals("克")){
            BigDecimal b = new BigDecimal(28.3495);
            BigDecimal divide = a.divide(b);
            BigDecimal d =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(d+"");

        }
        if (str2.equals("微克")){
            BigDecimal b = new BigDecimal(28349523.125);
            BigDecimal divide = a.multiply(b);
            BigDecimal d =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(d+"");

        }
        if (str2.equals("毫克")){
            BigDecimal b = new BigDecimal(28349.523125);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(3, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("盎司")){
            edt2.setText(a +"");

        }
        if (str2.equals("克拉")){
            BigDecimal b = new BigDecimal(141.7476156);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(3, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //"毫克 mg"
    private void initValmg(){
        if (str2.equals("吨")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.divide(b);
            BigDecimal d =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(d+"");
        }
        if (str2.equals("千克")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.divide(b);
            BigDecimal d =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(d+"");

        }
        if (str2.equals("克")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("微克")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("毫克")){
            edt2.setText(a +"");

        }
        if (str2.equals("盎司")){
            BigDecimal b = new BigDecimal(0.0000353);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("克拉")){
            BigDecimal b = new BigDecimal(0.005);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //"微克 μg"
    private void initValμg(){
        if (str2.equals("吨")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.divide(b);
            BigDecimal c = new BigDecimal(1000);
            BigDecimal divide1 = divide.divide(c);
            BigDecimal d =divide1.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(d+"");
        }
        if (str2.equals("千克")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.divide(b);
            BigDecimal d =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(d+"");

        }
        if (str2.equals("克")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("微克")){
            edt2.setText(a +"");

        }
        if (str2.equals("毫克")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("盎司")){
            BigDecimal b = new BigDecimal(0.000000035274);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("克拉")){
            BigDecimal b = new BigDecimal(0.000005);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }


    //"克 g"
    private void initValg(){
        if (str2.equals("吨")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("千克")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("克")){
            edt2.setText(a +"");

        }
        if (str2.equals("微克")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫克")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("盎司")){
            BigDecimal b = new BigDecimal(0.035274);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(3, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("克拉")){
            BigDecimal b = new BigDecimal(5);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
    }

    //"千克 kg"
    private void initValkg(){
        if (str2.equals("吨")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("千克")){
            edt2.setText(a +"");

        }
        if (str2.equals("克")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微克")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫克")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("盎司")){
            BigDecimal b = new BigDecimal(35.274);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(3, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("克拉")){
            BigDecimal b = new BigDecimal(5000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
    }

    //吨->
    private void initValt(){
        if (str2.equals("吨")){
            edt2.setText(a +"");
        }
        if (str2.equals("千克")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("克")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微克")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.multiply(b);
            BigDecimal c = new BigDecimal(1000);
            BigDecimal divide1 = c.multiply(divide);
            edt2.setText(divide1+"");
        }
        if (str2.equals("毫克")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("盎司")){
            BigDecimal b = new BigDecimal(35273.962);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(3, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("克拉")){
            BigDecimal b = new BigDecimal(5000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
    }

    //第一个下拉列表
    private void initItemValue1() {
        String[] str = {"吨 t","千克 kg", "克 g", "微克 μg","毫克 mg","盎司 oz","克拉 ct"};
        List<Strings> stringsList = new ArrayList<>();
        for(int i = 0; i < str.length; i++) {
            Strings strings1 = new Strings();
            strings1.setNum(str[i]);
            stringsList.add(strings1);
        }
        item1Adapter1 = new LengthItem1Adapter(stringsList,getBaseContext());
        lengthItemRec1.setAdapter(item1Adapter1);

        item1Adapter1.MyItemOnclick(new MyItemOnclick() {
            @Override
            public void onItemClick(View view, int position, String str) {
                value1 = str;

                str2 = txt2.getText().toString();
                a = new BigDecimal(num1);
                if (value1.equals("吨 t")){
                    dialog1.dismiss() ;
                    unit1.setText("");
                    unit1.setText("t");
                    txt1.setText("吨");
                    initValt();
                }
                if (value1.contains("千克 kg")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("kg");
                    txt1.setText("千克");
                    initValkg();

                }
                if (value1.contains("克 g")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("g");
                    txt1.setText("克");
                    initValg();
                }
                if (value1.contains("微克 μg")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("μg");
                    txt1.setText("微克");
                    initValμg();

                }
                if (value1.contains("毫克 mg")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("mg");
                    txt1.setText("毫克");
                    initValmg();

                }
                if (value1.contains("盎司 oz")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("oz");
                    txt1.setText("盎司");
                    initValoz();

                }
                if (value1.contains("克拉 ct")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("ct");
                    txt1.setText("克拉");
                    initValct();

                }

            }
        });
    }
    //第二个下拉列表
    private void initItemValue2() {
        String[] str = {"吨 t","千克 kg", "克 g", "微克 μg","毫克 mg","盎司 oz","克拉 ct"};
        List<Strings> stringsList = new ArrayList<>();
        for(int i = 0; i < str.length; i++) {
            Strings strings1 = new Strings();
            strings1.setNum(str[i]);
            stringsList.add(strings1);
        }
        item1Adapter2 = new LengthItem1Adapter(stringsList,getBaseContext());
        lengthItemRec2.setAdapter(item1Adapter2);

        item1Adapter2.MyItemOnclick(new MyItemOnclick() {
            @Override
            public void onItemClick(View view, int position, String str) {
                value2 = str;

                if (value2.equals("吨 t")){
                    dialog2.dismiss();
                    txt2.setText("吨");
                    unit2.setText("t");
                    initStart();

                }
                if (value2.contains("千克 kg")){
                    dialog2.dismiss();
                    txt2.setText("千克");
                    unit2.setText("kg");
                    initStart();

                }
                if (value2.contains("克 g")){
                    dialog2.dismiss();
                    txt2.setText("克");
                    unit2.setText("g");
                    initStart();
                }
                if (value2.contains("微克 μg")){
                    dialog2.dismiss();
                    txt2.setText("微克");
                    unit2.setText("μg");
                    initStart();

                }
                if (value2.contains("毫克 mg")){
                    dialog2.dismiss();
                    txt2.setText("毫克");
                    unit2.setText("mg");
                    initStart();

                }

                if (value2.contains("盎司 oz")){
                    dialog2.dismiss();
                    txt2.setText("盎司");
                    unit2.setText("oz");
                    initStart();

                }
                if (value2.contains("克拉 ct")){
                    dialog2.dismiss();
                    txt2.setText("克拉");
                    unit2.setText("ct");
                    initStart();
                }

            }
        });
    }

    /**获取EditText光标所在的位置*/
    private int getEditTextCursorIndex(EditText mEditText){
        return mEditText.getSelectionStart();
    }

    /**向EditText指定光标位置插入字符串*/
    private void insertText(EditText mEditText, String mText){
        mEditText.getText().insert(getEditTextCursorIndex(mEditText), mText);
    }

}
